/**
 * pinia的存储区域
 */

import { defineStore } from "pinia";
import WebStorageCache from "web-storage-cache";

const cache = new WebStorageCache({
  storage: window.localStorage,
});

export const mainStore = defineStore("main", {
  state() {
    return {
      token: null,
      loginUserInfo: null,
      isCollapse: false,
      historyList: [],
    };
  },
  //pinia没有mutations，省略了这个操作
  actions: {
    setToken(token) {
      this.token = token;
    },
    setLoginUserInfo(loginUserInfo) {
      this.loginUserInfo = loginUserInfo;
    },
    setIsCollapse(flag) {
      this.isCollapse = flag;
    },
    logOut() {
      this.token = null;
      this.loginUserInfo = null;
    },

    //历史记录列表
    addHistory(item) {
      //我们希望在添加的时候不要重复
      let index = this.historyList.findIndex(
        (_item) => _item.routeName === item.routeName
      );
      if (index === -1) {
        this.historyList.push(item);
      }
    },
    //删除历史记录
    removeHistoryByRouteName(routeName) {
      let index = this.historyList.findIndex(
        (item) => item.routeName == routeName
      );
      if (index != -1) {
        this.historyList.splice(index, 1);
      }
    },
  },
  //配置状态的持久化
  persist: {
    key: "ele-manger-store",
    storage: {
      getItem: (key) => cache.get(key),
      setItem: (key, value) => cache.set(key, value, { exp: 60 * 60 * 24 }),
      removeItem: (key) => cache.remove(key),
      clear: () => cache.clear(),
    },
    //指定需要持久化的字段
    paths: ["token", "loginUserInfo"],
  },
});
